/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.core.models.application;

import org.eclipse.core.resources.IProject;
import test.org.zoundry.gaedt.core.ZGaeCoreTestCase;

/**
 * Test case for the gae application model.
 */
public class ZGaeApplicationModelTest extends ZGaeCoreTestCase
{
    /**
     * Tests the application model.
     *
     * @throws Exception
     */
    public void testEmptyApplicationModel() throws Exception
    {
        IProject project = createTestProject("ZGaeApplicationModelTest_testEmptyApplicationModel");

        ZGaeApplicationModel model = new ZGaeApplicationModel(project);
        assertNotNull(model);
        assertModelIsEmpty(model);

        populateBaseModelInfo(model);
        populateDevModelInfo(model);

        assertModelBaseSettings(model);
        assertModelDevSettings(model);
    }

    /**
     * Tests the application model save/load.
     *
     * @throws Exception
     */
    public void testApplicationModelIO() throws Exception
    {
        IProject project = createTestProject("ZGaeApplicationModelTest_testApplicationModelIO");

        ZGaeApplicationModel model = new ZGaeApplicationModel(project);
        assertNotNull(model);

        populateBaseModelInfo(model);
        populateDevModelInfo(model);

        model.save();

        model = new ZGaeApplicationModel(project);
        assertNotNull(model);
        assertModelBaseSettings(model);
        assertModelDevSettings(model);
    }

    /**
     * Asserts that the model is empty.
     *
     * @param aModel
     */
    protected void assertModelIsEmpty(ZGaeApplicationModel aModel)
    {
        assertEquals("", aModel.getName());
        assertEquals("", aModel.getRuntimeType());
        assertEquals(1, aModel.getApiVersion());
        assertEquals(1, aModel.getVersion());
        assertEquals("", aModel.getDevAddress());
        assertEquals("", aModel.getDevAuthDomain());
        assertEquals(false, aModel.isDevDebug());
        assertEquals(false, aModel.isDevDebugImports());
        assertEquals(false, aModel.isDevDisableStaticCaching());
        assertEquals(false, aModel.isDevEnableSendmail());
        assertEquals(8080, aModel.getDevPort());
        assertEquals(false, aModel.isDevRequireIndexes());
        assertEquals(false, aModel.isDevShowMailBody());
        assertEquals("", aModel.getDevSMTPHost());
        assertEquals("", aModel.getDevSMTPPassword());
        assertEquals(25, aModel.getDevSMTPPort());
        assertEquals("", aModel.getDevSMTPUser());
    }

    /**
     * Asserts that the model has the correct base settings.
     *
     * @param aModel
     */
    protected void assertModelBaseSettings(ZGaeApplicationModel aModel)
    {
        assertEquals("testApp", aModel.getName());
        assertEquals("python", aModel.getRuntimeType());
        assertEquals(5, aModel.getApiVersion());
        assertEquals(3, aModel.getVersion());
    }

    /**
     * Asserts that the model has the correct dev settings.
     *
     * @param aModel
     */
    protected void assertModelDevSettings(ZGaeApplicationModel aModel)
    {
        assertEquals("0.0.0.0", aModel.getDevAddress());
        assertEquals("mydomain.com", aModel.getDevAuthDomain());
        assertEquals(true, aModel.isDevDebug());
        assertEquals(true, aModel.isDevDebugImports());
        assertEquals(true, aModel.isDevDisableStaticCaching());
        assertEquals(true, aModel.isDevEnableSendmail());
        assertEquals(8088, aModel.getDevPort());
        assertEquals(true, aModel.isDevRequireIndexes());
        assertEquals(true, aModel.isDevShowMailBody());
        assertEquals("mysmtphost.com", aModel.getDevSMTPHost());
        assertEquals("mypassword", aModel.getDevSMTPPassword());
        assertEquals(8877, aModel.getDevSMTPPort());
        assertEquals("myuser", aModel.getDevSMTPUser());
    }

    /**
     * Populate the model with some info.
     *
     * @param aModel
     */
    protected void populateBaseModelInfo(ZGaeApplicationModel aModel)
    {
        aModel.setName("testApp");
        aModel.setRuntimeType("python");
        aModel.setApiVersion(5);
        aModel.setVersion(3);
    }

    /**
     * Populate the model with some dev settings.
     *
     * @param aModel
     */
    protected void populateDevModelInfo(ZGaeApplicationModel aModel)
    {
        aModel.setDevAddress("0.0.0.0");
        aModel.setDevAuthDomain("mydomain.com");
        aModel.setDevDebug(true);
        aModel.setDevDebugImports(true);
        aModel.setDevDisableStaticCaching(true);
        aModel.setDevEnableSendmail(true);
        aModel.setDevPort(8088);
        aModel.setDevRequireIndexes(true);
        aModel.setDevShowMailBody(true);
        aModel.setDevSMTPHost("mysmtphost.com");
        aModel.setDevSMTPPassword("mypassword");
        aModel.setDevSMTPPort(8877);
        aModel.setDevSMTPUser("myuser");
    }
}
